#include "Controller.h"
#include "Requisition.h"
#include "Scheduler.h"
#include <iostream>
using namespace std;

Controller::Controller(Scheduler* s, Disk* d, PoissonGenerator * pg, double lb)
{
	scheduler = s;
	disk = d;
	hasPendingReqs = true;
	poissonGenerator = pg;
	lambda = lb;
}

Controller::~Controller()
{
}

double Controller::treatReq()
{
	double timeTaken;
	Requisition req = scheduler->next(disk->getPos());
	timeTaken = disk->treatReq(req);
	if (hasPendingReqs) {
		hasPendingReqs = poissonGenerator->requeue(timeTaken*lambda, scheduler);
	}
	else {
//		cout << "cabou" << endl;
	}
	//cout << "Indo para: " << req.track << ", " << req.sector << endl;  
	//cout << "Demorou: " << timeTaken << endl << endl;
	return timeTaken;
}

Position Controller::addressConv(int memPos)
{
	//cout << "MemPos: " << memPos << endl;
	int bytesPerTrack = (disk->bytesPerSector)*(disk->sectorsPerTrack);
	//cout << "bytesPerTrack: " << bytesPerTrack << endl;
	int tracks = memPos/bytesPerTrack;
	//cout << "track: " << tracks << endl;
	int sector = (memPos/disk->bytesPerSector)%disk->sectorsPerTrack;
	//cout << "sector: " << sector << endl;
	Position p;
	p.sector = sector;
	p.track = tracks;
	return p;
	
}